package com.hgk.wanwan.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.common.dongqiuhui.apps.comm.constants.UrlConstans;
import com.common.dongqiuhui.apps.comm.util.SharedPreferencesUtil;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase;
import com.common.handmark.pulltorefresh.library.PullToRefreshListView;
import com.common.handmark.pulltorefresh.library.internal.BallBaseAdapter;
import com.hgk.common.common.constants.DataConstants;
import com.hgk.common.common.ui.activity.UpAndDownRefreshActivity;
import com.hgk.wanwan.search.adapter.SearchAdapter;
import com.hgk.wanwan.search.model.SearchBean;

import java.util.HashMap;
import java.util.Map;

import width.hgk.wanwan.R;

public class SearchActivity extends UpAndDownRefreshActivity implements TextWatcher {

    private final String GET_SEARCH_URL = UrlConstans.ZWCOMMOMURL+"/domain/tab1/search.json";//搜索接口
    //搜索历史(未登录状态移动端自己记录，登录状态才调用接口请求)
    private final String GET_SEARCH_HISTORY_URL = UrlConstans.ZWCOMMOMURL+"/domain/tab1/search_history.json";
    private String mTypes = "1";//搜索类型(1.搜索用户，一次返回20条,昵称、标签、一句话自我介绍字段根据搜索关键字标红)(后续会继续添加)
    private String mUserId;//userid(未登录不传)
    private String mQuery;//搜索关键字
    private String mLastId;//第一次不传或者传''，用于翻页

    private PullToRefreshListView mPullToRefreshListView;
    private EditText mEtSearch;
    private ImageView mIvIcon,mIvDelete;
    private Button mBtnCursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_search);
        mUserId = SharedPreferencesUtil.getStringValue(mContext, DataConstants.USERID);
        mTypes = getIntent().getStringExtra("type");
        initView();
    }

    private void initView() {
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        mEtSearch = (EditText) findViewById(R.id.edit_location_search);
        mIvDelete = (ImageView) findViewById(R.id.iv_del);
        mBtnCursor = (Button) findViewById(R.id.btn_cancel);

        mEtSearch.addTextChangedListener(this);
    }

    @Override
    protected BallBaseAdapter getAdapte() {
        SearchAdapter adapter = new SearchAdapter(mContext);
        adapter.setFrom(mTypes);
        return adapter;
    }

    @Override
    protected PullToRefreshBase.Mode onCreate() {
        return null;
    }

    @Override
    protected void onDownRefresh() {

    }

    @Override
    protected void onUpRefresh() {

    }

    @Override
    protected Map getApiParams(String action) {
        Map<String ,String> params = new HashMap<>();
        if (action.equals("search_user")) {
            params.put(DataConstants.SEARCH_TYPE, mTypes);
            params.put(DataConstants.USERID, mUserId);
        }
        return null;
    }

    @Override
    protected void success(String action, Object result) {

    }

    @Override
    protected void fail(String action, String failStr) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void setLeftButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setRightButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public Object getTitleString() {
        return null;
    }

    @Override
    public int getBodyView() {
        return R.layout.activity_search;
    }

    public static void launch(Context context,String type){
        Intent intent = new Intent(context,SearchActivity.class);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    /**搜索框中的文字变更监听事件*/
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        //文本变化前无操作
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //文本变化后shi进行操作
        getNetDataForPost("search_user",GET_SEARCH_URL, SearchBean.class);

    }

    @Override
    public void afterTextChanged(Editable s) {
        //变化hou无操作
    }
}
